package float
{
   import flash.display.DisplayObjectContainer;
   import flash.display.MovieClip;
   
   import jp.nium.display.getInstanceById;
   import jp.progression.scenes.SceneId;
   import jp.progression.scenes.SceneObject;
   
   /**
    * @author  cjboy1984@gmail.com
    * @usage
    *
    * import float.*;
    *
    * 1. Show
    *    (a) Remember to set the base class to BaseFloat* (depends on functionality).
    *
    *    MyFloat.add('The_id_of_the_instance');
    *
    * -----------------------------------------------------------------------------------
    * 1. Hide
    *
    *    MyFloat.remove(); // Only Remove the topmost pop-up.
    *    MyFloat.removeAll(); // Remove all the pop-ups.
    *
    * @see
    * 原理：動態addScene
    * a. 先在目前的scene後面加個FloatScene，該scene裡有個cast是從CastMovieClip繼承而來(通常是BaseFloat -> CastMovieClip)
    * b. 指定manager.goto(a步驟新增出來的scene)
    */
   public class MyFloat
   {
      public function MyFloat(pvt:PrivateClass)
      {
         // DONOT try to new it!!!
      }
      
      // --------------------- LINE ---------------------
      
      /**
       * Add The pop-up.
       * @param   $container     - The container.
       * @param	givenId        - The calss name defined in *.fla.
       * @param	layer_index    - If you want it not just pop up in the top, give this value.
       */
      public static function add($container:DisplayObjectContainer, givenId:String, layer_index:int = -1):void
      {
         // get the cast.
         var cast:MovieClip = MovieClip(getInstanceById(givenId));
         if (!cast) return;
         
         // Check if duplicated scene.
         var sub_scene:SceneObject;
         if (!(sub_scene = DEF.mgr.current.getSceneByName(FloatScene.NAME)))
         {
            sub_scene = DEF.mgr.current.addScene(new FloatScene($container, cast, layer_index));
         }
         
         // local -> global (VERY IMPORTANT)
         DEF.mgr.goto(sub_scene.localToGlobal(sub_scene.sceneId));
      }
      
      /**
       * Only Remove the topmost pop-up.
       */
      public static function remove():void
      {
         var parentScene:SceneObject = DEF.mgr.current.parent;
         // local -> global (VERY IMPORTANT)
         DEF.mgr.goto(parentScene.localToGlobal(parentScene.sceneId));
      }
      
      /**
       * Remove all the pop-ups.
       */
      public static function removeAll():void
      {
         var globalSceneId:SceneId = DEF.mgr.current.localToGlobal(DEF.mgr.currentSceneId);
         var newPath:String = globalSceneId.path.replace(/\/float*/g, "");
         DEF.mgr.goto(new SceneId(newPath));
      }
      
      // ################### protected ##################
      
      // #################### private ###################
      
      // --------------------- LINE ---------------------
      
   }
   
}

class PrivateClass
{
   public function PrivateClass()
   {
      trace("PrivateClass.PrivateClass()");
   }
}
